const {prisma, minioClient, mqttClient}=require('../controllers/utils.js')

// 数据库元数据导入
level1=[
    {id:0, name:"关键部件"},
    {id:1, name:"关键软件"},
    {id:2, name:"设计集成"},
    {id:3, name:"加工生产"},
    {id:4, name:"检测认证"},
    {id:5, name:"场景方案"},
    {id:6, name:"金融服务"}
]

level2=[
    {id:0, level1_id:0, name:"传动装置"},
    {id:1, level1_id:0, name:"控制器"},
    {id:2, level1_id:0, name:"伺服电机"},
    {id:3, level1_id:0, name:"传感器"},
    {id:4, level1_id:0, name:"芯片"},
    {id:5, level1_id:0, name:"能源系统"},
    {id:6, level1_id:0, name:"其它"},
    {id:7, level1_id:1, name:"感知"},
    {id:8, level1_id:1, name:"认知"},
    {id:9, level1_id:1, name:"决策"},
    {id:10, level1_id:1, name:"表达"},
    {id:11, level1_id:1, name:"运动控制"},
    {id:12, level1_id:1, name:"操作系统"},
    {id:13, level1_id:1, name:"其它"},
    {id:14, level1_id:2, name:"工业机器人"},
    {id:15, level1_id:2, name:"服务机器人"},
    {id:16, level1_id:2, name:"特种机器人"},
    {id:17, level1_id:3, name:"ODM"},
    {id:18, level1_id:3, name:"OEM"},
    {id:19, level1_id:3, name:"OBM"},
    {id:20, level1_id:3, name:"其它"},
    {id:21, level1_id:4, name:"3C"},
    {id:22, level1_id:4, name:"SRRC"},
    {id:23, level1_id:4, name:"CTA"},
    {id:24, level1_id:4, name:"CR"},
    {id:25, level1_id:4, name:"其它"},
    {id:26, level1_id:5, name:"工业场景"},
    {id:27, level1_id:5, name:"商业场景"},
    {id:28, level1_id:5, name:"社区场景"},
    {id:29, level1_id:5, name:"家庭场景"},
    {id:30, level1_id:5, name:"医疗场景"},
    {id:31, level1_id:5, name:"教育场景"},
    {id:32, level1_id:5, name:"特种领域"},
    {id:33, level1_id:5, name:"农业领域"},
    {id:34, level1_id:5, name:"建造领域"},
    {id:35, level1_id:5, name:"其它场景"},
    {id:36, level1_id:6, name:"股权融资"},
    {id:37, level1_id:6, name:"债权融资"},
    {id:38, level1_id:6, name:"并购重组"},
    {id:39, level1_id:6, name:"IPO"},
    {id:40, level1_id:6, name:"ICO"},
    {id:41, level1_id:6, name:"其它"}
]

// const {PrismaClient}=require('@prisma/client')
// const prisma=new PrismaClient()
const initDatabase=async()=>{
    //导入元数据
    await prisma.level1.createMany({data:level1})
    await prisma.level2.createMany({data:level2})
    //导入测试数据
    await prisma.organization.create({
        data:{
            id:0,
            level2s:{connect:{id:0}},
            organization_type:"ENTERPRISE",
            name:'重庆具身智能0号公司',
            id_code:'000000000000000000',
            address:'渝中区解放碑',
            introduce:`重庆具身智能0号公司是行业领先的3D视觉感知整体技术方案提供商。公司已构建起“全栈式技术研发能力+全领域技术路线布局”的 3D视觉感知技术体系，在技术纵向上对包括深度引擎芯片、感光芯片、专用光学系统等在内的核心底层技术以及SDK、行业应用算法等全链路技术进行全栈式自主研发，在技术横向上对结构光、iToF、双目、dToF、LiDAR、工业三维测量进行全领域布局。以“让所有终端都能看懂世界”为使命，
            0号公司坚持“3D 视觉传感器+自主底层核心技术+完整应用方案”的市场定位，致力于将3D视觉感知技术应用于“衣、食、住、行、工、娱、医”等领域；在智能手机、线下零售等市场已率先突破百万级出货量，在生物识别、消费电子、AIoT、工业三维测量等市场实现多项商业应用，服务全球超过1000家客户及众多开发者。`
        }
    })
    await prisma.user.create({
        data:{
            id:0,
            talent:{
                create:{
                    id:0,
                    level2s:{
                        connect:[{id:0},{id:1}]
                    },
                    titles:["博士","教授"],
                    achievements:"在国际顶级期刊发表论文30余篇。"
                }
            },
            organization_id:0,
            organization_admin:true,
            admin:true,
            auditor:true,
            reporter:true,
            
            name:"admin",
            id_code:"000000000000000000",
            mobile:"00000000000",
            email:"admin@china.com",
            password:"test"
        }
    })
    await prisma.product.create({
        data:{
            id:0,
            organization_id:0,
            level2:{connect:[{id:0},{id:1}]},
            name:"擎天柱机器人",
            description:"特斯拉人形机器人"
        }
    })
    await prisma.service.create({
        data:{
            id:0,
            organization_id:0,
            level2:{connect:[{id:0},{id:1}]},
            name:"检测认证",
            description:"3C检测"
        }
    })
    console.log("初始化完毕")
}

initDatabase()

// 初始化minio
minioClient.bucketExists('talent-avatar',(err,exists)=>{
    if(err){
        console.error(err)
        return
    }
    if(exists){
        console.log('minio:talent-avatar exists')
        minioClient.setBucketPolicy('talent-avatar',JSON.stringify({
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "PublicReadGetObject",
                    "Effect": "Allow",
                    "Principal": "*",
                    "Action": [
                        "s3:GetObject"
                    ],
                    "Resource": [
                        "arn:aws:s3:::talent-avatar/*"
                    ]
                }
            ]
        }), err=>{
            if(err){
                console.error(err)
                return
            }
            console.log('minio:talent-avatar配置成功。')
        })
        return
    }else{
        minioClient.makeBucket('talent-avatar',(err)=>{
            if(err){
                console.error(err)
                return
            }
            minioClient.setBucketPolicy('talent-avatar',JSON.stringify({
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "PublicReadGetObject",
                        "Effect": "Allow",
                        "Principal": "*",
                        "Action": [
                            "s3:GetObject"
                        ],
                        "Resource": [
                            "arn:aws:s3:::talent-avatar/*"
                        ]
                    }
                ]
            }), err=>{
                if(err){
                    console.error(err)
                    return
                }
                console.log('minio:talent-avatar创建并配置成功。')
            })
        })
    }
})

minioClient.bucketExists('organization',(err,exists)=>{
    if(err){
        console.error(err)
        return
    }
    if(exists){
        console.log('minio:organization exists')
        minioClient.setBucketPolicy('organization',JSON.stringify({
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "PublicReadGetObject",
                    "Effect": "Allow",
                    "Principal": "*",
                    "Action": [
                        "s3:GetObject"
                    ],
                    "Resource": [
                        "arn:aws:s3:::organization/*"
                    ]
                }
            ]
        }), err=>{
            if(err){
                console.error(err)
                return
            }
            console.log('minio:organization配置成功。')
        })
        return
    }else{
        minioClient.makeBucket('organization',(err)=>{
            if(err){
                console.error(err)
                return
            }
            minioClient.setBucketPolicy('organization',JSON.stringify({
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "PublicReadGetObject",
                        "Effect": "Allow",
                        "Principal": "*",
                        "Action": [
                            "s3:GetObject"
                        ],
                        "Resource": [
                            "arn:aws:s3:::organization/*"
                        ]
                    }
                ]
            }), err=>{
                if(err){
                    console.error(err)
                    return
                }
                console.log('minio:organization创建并配置成功。')
            })
        })
    }
})

minioClient.bucketExists('information',(err,exists)=>{
    if(err){
        console.error(err)
        return
    }
    if(exists){
        console.log('minio:information exists')
        minioClient.setBucketPolicy('information',JSON.stringify({
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "PublicReadGetObject",
                    "Effect": "Allow",
                    "Principal": "*",
                    "Action": [
                        "s3:GetObject"
                    ],
                    "Resource": [
                        "arn:aws:s3:::information/*"
                    ]
                }
            ]
        }), err=>{
            if(err){
                console.error(err)
                return
            }
            console.log('minio:information配置成功。')
        })
        return
    }else{
        minioClient.makeBucket('information',(err)=>{
            if(err){
                console.error(err)
                return
            }
            minioClient.setBucketPolicy('information',JSON.stringify({
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "PublicReadGetObject",
                        "Effect": "Allow",
                        "Principal": "*",
                        "Action": [
                            "s3:GetObject"
                        ],
                        "Resource": [
                            "arn:aws:s3:::information/*"
                        ]
                    }
                ]
            }), err=>{
                if(err){
                    console.error(err)
                    return
                }
                console.log('minio:information创建并配置成功。')
            })
        })
    }
})